package com.manager.base;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.web.context.ServletContextAware;

import com.manager.pojo.system.SysUser;
import com.manager.util.CONSTANT;
import com.manager.util.Log;
import com.manager.util.Stringbuffer;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware ,ServletContextAware{
	protected BaseService baseService;
	
	protected HttpServletRequest request;

	protected HttpServletResponse response;
	
	protected ServletContext servletContext;
	
	//-------------------------多文件上传相关开始-------------------------
	protected File file[];
	protected String[] fileContentType;
	protected String[] fileFileName;
	
	protected String realBasePath;
	
	/*
	 * 取得项目所在目录的绝对根路径，配合文件上传用
	 * 取到的路径格式如下：
	 * E:/workspace/managerJDBCTemplateSSH2/WebRoot/
	 */
	protected String getRealBasePath(){
		if(this.realBasePath == null){
			this.realBasePath = request.getSession().getServletContext().getRealPath("").replaceAll("\\\\", "/");
		}
		return this.realBasePath;
	}
	
	protected void copyFile(File src, File dst) {
		int bufferSize = 1024 * 2;
		InputStream in = null;
		OutputStream out = null;
		
		try{
			in = new BufferedInputStream(new FileInputStream(src), bufferSize);
			out = new BufferedOutputStream(new FileOutputStream(dst), bufferSize);
			byte[] buffer = new byte[bufferSize];
			while (in.read(buffer) > 0) {
				out.write(buffer);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	// -------------------------多文件上传相关结束-------------------------

	/*
	 * 调用PrintWriter一次性输出全部信息
	 */
	protected void print(String content){
		PrintWriter out = null;
		try{
			out = response.getWriter();
			out.print(content);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.flush();
			out.close();
		}
	}
	
	//取得登录人员的id
	protected int getLoginUserId(){
		HttpSession session = request.getSession();
		SysUser user = (SysUser)session.getAttribute(CONSTANT.SESSION_KEY);
		return user.getId().intValue();
	}
	
	// 通用的服务器端跳转动作
	protected String forward(String url) {
		Stringbuffer sb = new Stringbuffer(servletContext.getRealPath(""));
		//Log.info("realPath:" + sb.toString());
		
		try {
			this.request.getRequestDispatcher(url).forward(request, response);
		} catch (Exception e) {
			Log.info("illegal path: " + sb.append("->").append(url).toString(), BaseAction.class);
			e.printStackTrace();
		}
		return Action.NONE;
	}
	
	// 通用的客户端跳转动作
	protected String sendRedirect(String url){
		try {
			response.sendRedirect(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Action.NONE;
	}
	
	/*
	 * getter and setter
	 */
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public File[] getFile() {
		return file;
	}

	public void setFile(File[] file) {
		this.file = file;
	}

	public String[] getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String[] fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String[] getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String[] fileFileName) {
		this.fileFileName = fileFileName;
	}
	
	
}
